package pass;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.util.NumberUtil;
import com.alibaba.fastjson.JSON;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author jukg
 * @date 2023/03/31 16:05
 * @description
 */
public class DateUtil {

    private static final Date STARTTIME = cn.hutool.core.date.DateUtil.parse("2000-01-01 00:00:00");


    public static void main(String[] args) {
        //generatorPageByDateDesc(2,10, null, null);
        //days();
        //testMonth();
       // System.out.println(cn.hutool.core.date.DateUtil.beginOfDay(cn.hutool.core.date.DateUtil.offsetDay(new Date(), -3)));
       // System.out.println(NumberUtil.decimalFormat("#.##%", (float)2/(float)9));
        //System.out.println(NumberUtil.decimalFormat("#.##%", ((float)2/(float)9)*100));
       /* List<String> list =null;
        for(String s : list){
            System.out.println(s);
        }*/

        testPageTime(5);

    }

    public static void testPageTime(int pageSize){
        Date start = null;
        Date end = null;
        Date date = new Date();
        start = start == null? cn.hutool.core.date.DateUtil.beginOfMonth(date): start;
        end = end == null? cn.hutool.core.date.DateUtil.endOfDay(date): end;
        long total = cn.hutool.core.date.DateUtil.between(start, end, DateUnit.DAY) + 1;
        int pages = (int) (total % pageSize == 0 ? total / pageSize : total / pageSize + 1);
        for(int i =0; i< pages; i++) {
            Date endTime = cn.hutool.core.date.DateUtil.offsetDay(end, -i * pageSize);
            Date startTime = cn.hutool.core.date.DateUtil.offsetDay(endTime, (-pageSize + 1));
            startTime = startTime.before(start) ? start : startTime;
            System.out.println(cn.hutool.core.date.DateUtil.beginOfDay(startTime));
            System.out.println(cn.hutool.core.date.DateUtil.endOfDay(endTime));
        }
    }

    public static void testMonth(){
        //Date date = cn.hutool.core.date.DateUtil.parse("2023-03-30 00:00:00", "yyyy-MM-dd HH:mm:ss");
        Date date = cn.hutool.core.date.DateUtil.lastMonth();
        Date s = cn.hutool.core.date.DateUtil.beginOfMonth(date);
        Date e = cn.hutool.core.date.DateUtil.endOfMonth(date);
        System.out.println(cn.hutool.core.date.DateUtil.format(date, "yyyy-MM-dd HH:mm:ss"));
        System.out.println(cn.hutool.core.date.DateUtil.format(s, "yyyy-MM-dd HH:mm:ss"));
        System.out.println(cn.hutool.core.date.DateUtil.format(e, "yyyy-MM-dd HH:mm:ss"));
    }

    public static void days(){
        Date end = new Date();
        Date start = cn.hutool.core.date.DateUtil.offsetDay(end, -19);
        System.out.println(cn.hutool.core.date.DateUtil.between(start, end, DateUnit.DAY));
        List<String> list = getDateList(start, end);
        System.out.println(JSON.toJSONString(list));

    }

    public static void generatorPageByDateDesc(int no, int size, Date s, Date e){

        int pageNo  = no;
        int pageSize = size;

        Date end = e;
        end = end == null? new Date(): end;
        //构造第一列时间
        Date start =s;
        start = start == null? STARTTIME: start;
        long total = cn.hutool.core.date.DateUtil.between(start, end, DateUnit.DAY);
        int pages = (int) (total % pageSize == 0 ? total / pageSize : total / pageSize + 1);
        Date endTime = cn.hutool.core.date.DateUtil.offsetDay(end, -pageSize * (pageNo-1));

        Date startTime = cn.hutool.core.date.DateUtil.offsetDay(endTime, -pageSize+1);

        startTime = startTime.before(start) ? start : startTime;
        List<String> list = getDateList(startTime, endTime);
        System.out.println("total:" + total + ", pages:" + pages + "------" + JSON.toJSONString(list));
    }


    public static void main1(String[] args) {
        Date yesterday = cn.hutool.core.date.DateUtil.yesterday().toJdkDate();
        Date date = new Date();
        DateTime date2 = cn.hutool.core.date.DateUtil.offsetDay(date, 0);
        System.out.println(getDateList(4, date2));
    }


    private static SimpleDateFormat dayFormat = new SimpleDateFormat("MM.dd");
    private static SimpleDateFormat monthFormat = new SimpleDateFormat("MM");

    public static List<String> getDateList(int type, Date date){
        List<String> result = new ArrayList<>();
        int intervels = 0;
        switch (type){
            case 1:
                intervels = 1;
                break;
            case 2:
                DateTime start  = cn.hutool.core.date.DateUtil.beginOfWeek(date);
                intervels = (int)cn.hutool.core.date.DateUtil.between(start, date, DateUnit.DAY);
                break;
            case 3:
                intervels = cn.hutool.core.date.DateUtil.dayOfMonth(date) -1;
                break;
            case 4:
                intervels = cn.hutool.core.date.DateUtil.month(date);
        }
        int i = intervels;
        while(i >= 0){
            String item = "";
            if(4 == type){
                DateTime dateTime = cn.hutool.core.date.DateUtil.offsetMonth(date, -i);
                item = monthFormat.format(dateTime);
            }else {
                DateTime dateTime = cn.hutool.core.date.DateUtil.offsetDay(date, -i);
                item = dayFormat.format(dateTime);
            }
            result.add(item.startsWith("0")? item.substring(1): item);
            i--;
        }
        return  result;
    }


    public static int intervelDays(Date start, Date end){

        long days = cn.hutool.core.date.DateUtil.between(start, end, DateUnit.DAY);

        return (int)days;
    }


    public static Date getStartTimeByType(int type, Date date){
        switch (type){
            case 1:
                return cn.hutool.core.date.DateUtil.beginOfDay(date);
            case 2:
                return cn.hutool.core.date.DateUtil.beginOfWeek(date);
            case 3:
                return cn.hutool.core.date.DateUtil.beginOfMonth(date);
            case 4:
                return cn.hutool.core.date.DateUtil.beginOfYear(date);
        }
        return null;
    }
    private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static void main2(String[] args) {
        Date date = new Date();
        System.out.println(format.format(getStartTimeByType(4, date)));
    }


    public static void main22(String[] args) {
        Date date = new Date();
        Date date2 = cn.hutool.core.date.DateUtil.offsetDay(date, 10);
        List<String> list = getDateList(date,date2);
        for(String d : list){
            System.out.println(d);
        }
    }


    private static SimpleDateFormat DAYOFYEARFORMAT = new SimpleDateFormat("yyyy-MM-dd");

    public static List<String> getDateList(Date start, Date end){
        List<String> result = new ArrayList<>();
        List<DateTime> list  = cn.hutool.core.date.DateUtil.rangeToList(start, end, DateField.DAY_OF_YEAR);
        for(int i = list.size()-1; i >= 0; i--){
            result.add(DAYOFYEARFORMAT.format(list.get(i)));
        }
        return  result;
    }

    public static void  page(){
        Date date = new Date();
        for(int i = 1; i < 5; i++) {
            int pageNo = i;
            int pageSize = 10;
            Date start = cn.hutool.core.date.DateUtil.beginOfDay(date);
            Date end = cn.hutool.core.date.DateUtil.offsetDay(start, 25);
            System.out.println("p1:" + format.format(start) + ", p2:" + format.format(end));
            long total = cn.hutool.core.date.DateUtil.between(start, end, DateUnit.DAY);
            int pages = (int) (total % pageSize == 0 ? total / pageSize : total / pageSize + 1);
            System.out.println("total:" + total +", pages:" + pages);
            if(pageNo > pages){
                System.out.println("超限");
                return;
            }
            Date s = cn.hutool.core.date.DateUtil.offsetDay(start, pageSize * (pageNo-1));
            Date e = cn.hutool.core.date.DateUtil.offsetDay(s, pageSize-1);
            e = e.after(end) ? end : e;
            System.out.println("start:" + format.format(s) + ", end:" + format.format(e));
        }

        //total:25
        //start:2023-04-06 00:00:00, end:2023-04-16 00:00:00


    }

    public static void main12(String[] args) {
        Date date = cn.hutool.core.date.DateUtil.parse("2000-01-01 00:00:00");
        System.out.println(cn.hutool.core.date.DateUtil.format(date, "yyyy-MM-dd HH:mm:sss"));
    }
}

